﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CarShop
{
    class CarShop
    {
        // some pre-entered vehicles in Dealership
        static readonly Vehicle[] database =  { 
            new Car("Honda", "Accord", "Grey", 1998, 240, 4000, new Engine(EngineType.Petrol, 2.0f, 136, 12, 200000), Transmission.manual),
            new Car("BMW", "320d", "Grey", 2000, 250, 8000, new Engine(EngineType.Diesel, 2.0f, 150, 12, 287000), Transmission.manual),
            new Car("Mercedes-Benz", "C220", "Black", 2005, 250, 12000, new Engine(EngineType.Diesel, 2.2f, 163, 12, 178000), Transmission.automatic),
            new Car("Mitsubishi", "Carisma", "Red", 1997, 155, 2800, new Engine(EngineType.Petrol, 1.6f, 66, 10, 195000), Transmission.manual),
            new Car("Fiat", "Brava", "Metallic Blue", 2001, 178, 4500, new Engine(EngineType.Petrol, 1.6f, 110, 12, 120000), Transmission.manual),
            new Car("Toyota", "Prius", "Silver", 2010, 145, 10800, new Engine(EngineType.Hybrid, 1.4f, 80, 4, 0), Transmission.manual),
            new Car("Smart", "Fortwo", "Yellow", 2012, 90, 22000, new Engine(EngineType.Electric, 1.2f, 71, 0, 0), Transmission.automatic),
            new Car("Ford", "Mustang Shelby GT500", "Red", 1967, 220, 45000, new Engine(EngineType.Petrol, 2.2f, 360, 15, 144000), Transmission.manual),
            new Car("DeLorean", "DMC", "White", 1981, 88, 30000, new Engine(EngineType.Diesel, 1.6f, 190, 4.5, 55000), Transmission.semiAutomatic),
            new Bus("Ikarus", "Red Arrow", "Red", 1960, 40, 1000, new Engine(EngineType.Petrol, 6.6f, 50, 50, 5000000), Transmission.manual, 100, 5300),
            new Bus("Routemaster", "Double Decker", "Red", 2011, 120, 400000, new Engine(EngineType.Diesel, 12.0f, 600, 12, 10000), Transmission.semiAutomatic, 48, 12500),
            new Minibus("Volkswagen", "Type 2", "Green", 1958, 120, 32000, new Engine(EngineType.Petrol, 2.2f, 100, 12.2, 250000), Transmission.manual, 7, 3000, DrivingWheel.FWD),
            new Minibus("Blue Bird", "All American", "Blue/White", 1968, 130, 42000, new Engine(EngineType.Petrol, 2.4f, 120, 15.2, 480000), Transmission.manual, 35, 8000, DrivingWheel.RWD),
            new Truck("IVECO", "TIR", "Black", 1999, 140, 200000, new Engine(EngineType.Petrol, 12.0f, 1200, 24, 100000), Transmission.semiAutomatic, 4, 12000, 25000),
        };

        // Method to add new vehicle to our dealership
        static Vehicle AddNewVehicle()
        {
            Console.WriteLine("What kind of vehicle?");
            Console.WriteLine("1. Car");
            Console.WriteLine("2. Bus");
            Console.WriteLine("3. Jeep");
            Console.WriteLine("4. Mini-bus");
            Console.WriteLine("5. Van");
            Console.WriteLine("6. Truck");
            Console.WriteLine("7. Motorcycle");
            Console.Write("Enter your choice (1-7): ");
            int menuOption = int.Parse(Console.ReadLine());

            switch (menuOption)
            {
                case 1: return AddCar();
                case 2: return AddCar();
                case 3: return AddCar();
                case 4: return AddCar();
                case 5: return AddCar();
                case 6: return AddCar();
                case 7: return AddCar();
                default: return AddCar();
            }
        }

        private static Vehicle AddCar()
        {
            Console.WriteLine();
            Console.Write("Enter car manufacturer: ");
            string manufacturer = Console.ReadLine();

            Console.Write("Enter car model: ");
            string model = Console.ReadLine();

            Console.Write("Enter car color: ");
            string carColor = Console.ReadLine();

            Console.Write("Enter year of production: ");
            int year = int.Parse(Console.ReadLine());

            Console.Write("Enter max speed: ");
            int maxSpeed = int.Parse(Console.ReadLine());

            Console.Write("Enter price: ");
            int carPrice = int.Parse(Console.ReadLine());

            Transmission trans = new Transmission();
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Choose transmission: ");
            sb.AppendLine("1. Manual");
            sb.AppendLine("2. Automatic");
            sb.AppendLine("3. Semi-automatic");

            Console.Write(sb.ToString());
            int transType = int.Parse(Console.ReadLine());
            trans = (Transmission)transType;
            Console.WriteLine();

            EngineType carEngine = new EngineType();

            StringBuilder myEngine = new StringBuilder();
            myEngine.AppendLine("Choose engine type: ");
            myEngine.AppendLine("1. Petrol");
            myEngine.AppendLine("2. Diesel");
            myEngine.AppendLine("3. Hybrid");
            myEngine.AppendLine("4. Electric");

            Console.Write(myEngine.ToString());
            int myEngineType = int.Parse(Console.ReadLine());
            carEngine = (EngineType)myEngineType;
            Console.WriteLine();
            Console.Write("Enter engine capacity: ");
            float capacity = float.Parse(Console.ReadLine());

            Console.Write("Enter engine horse power: ");
            int horsePower = int.Parse(Console.ReadLine());

            Console.Write("Enter fuel consumption: ");
            int expense = int.Parse(Console.ReadLine());

            Console.Write("Enter engine mileage if the car is used: ");
            int mileageOfEngine = 0;

            string mileage = Console.ReadLine();
            if (mileage != string.Empty)
            {
                mileageOfEngine = int.Parse(mileage);
            }
            Engine myCarEngine = new Engine(carEngine, capacity, horsePower, expense, mileageOfEngine);
            Car newCar = new Car(manufacturer, model, carColor, year, maxSpeed, carPrice, myCarEngine, trans);

            Console.WriteLine("Would you like to add some extras as a description? (Y/N)");
            string enteredChoice = Console.ReadLine().ToLower();

            if (enteredChoice == "y" || enteredChoice == "yes")
            {
                Console.WriteLine("Press Enter twice to stop.");
                string extraDescription = Console.ReadLine();

                while (extraDescription != string.Empty)
                {
                    newCar.AddComment(extraDescription);
                    extraDescription = Console.ReadLine();
                }
            }
            Console.WriteLine();
            return newCar;
        }

        // Remove sold car.
        static int RemoveSoldVehicle()
        {
            Console.Write("Enter car ID for the car you want to remove: ");
            int carID = int.Parse(Console.ReadLine());
            Console.WriteLine();
            return carID;
        }

        // Display all available vehicles
        static void DisplayAllCars(Dealership<Vehicle> stockCars)
        {
            int uniqueID = 1;
            Thread.CurrentThread.CurrentCulture =
                   CultureInfo.GetCultureInfo("bg-BG");
            foreach (var item in stockCars)
            {
                if (item != null)
                {
                    Console.WriteLine();
                    Console.WriteLine("Vehicle ID: {0}", uniqueID);
                    Console.WriteLine(item.ToString());
                }
                uniqueID++;
            }
        }

        //Displays whole info about a specific vehicle
        static void DisplayWholeInfo(Dealership<Vehicle> stockCars)
        {
            Console.Write("Enter Vehicle ID (1 - {0}): ", stockCars.Count);
            int id = int.Parse(Console.ReadLine());
            Console.WriteLine();
            Console.WriteLine(stockCars[id - 1]);
            Console.WriteLine(stockCars[id - 1].AdditionalInfo());
        }

        static void Main()
        {
            try
            {
                //Creating some cars in my dealership
                Dealership<Vehicle> myDealerShip = new Dealership<Vehicle>();
                foreach (var vehicle in database)
                {
                    myDealerShip.AddVehicle(vehicle);
                }
                
                Subscriber sub1 = new Subscriber("SUB #1", myDealerShip);
                Subscriber sub2 = new Subscriber("sub2", myDealerShip);

                int choice = -1;

                do
                {
                    Console.WriteLine("What would you like to do?");
                    Console.WriteLine("1. Add vehicle to your dealership.");
                    Console.WriteLine("2. Remove vehicle from your deelership (sold it!).");
                    Console.WriteLine("3. View all vehicles in your dealership. (display in order they were added)");
                    Console.WriteLine("4. Detailed view of a specific vehicle.(By ID)");
                    Console.WriteLine("5. Enter e subscriber for your dealership.");
                    Console.Write("Enter your choice (1-5): ");
                    choice = int.Parse(Console.ReadLine());

                    switch (choice)
                    {
                        case 1: myDealerShip.AddVehicle(AddNewVehicle()); break;
                        case 2: myDealerShip.RemoveVehicle(RemoveSoldVehicle() - 1); break;
                        case 3: DisplayAllCars(myDealerShip); break;
                        case 4: DisplayWholeInfo(myDealerShip); break;
                        case 5: Console.Write("Please enter your name: "); Subscriber newSub = new Subscriber(Console.ReadLine(), myDealerShip); break;
                        default: Console.WriteLine("Bye - Bye");
                            break;
                    }

                    Console.Write("Want to continiue? (Y/N) ");
                    string userChoice = Console.ReadLine().ToLower();

                    if (userChoice == "y" || userChoice == "yes")
                    {
                        Console.Clear();
                    }
                    else { break; }
                }
                while (choice > 0 && choice <= 5);
            }
            catch (DealershipException ex)
            {
                Console.WriteLine(ex.DealershipMessage);
            }
            catch (SystemException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}